/**
* @ClassName: CredentialsDaoImpl
* @Description: 
* @author YuYang
* @date 2013-6-5 上午12:45:31
*/
package com.demodis.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.demodis.dao.CredentialsDaoI;
import com.demodis.domain.TblCredentials;
import com.demodis.util.DBUtil;

public class CredentialsDaoImpl extends BaseDao implements CredentialsDaoI {

	@Override
	public boolean save(TblCredentials t) {
		String sql = "INSERT INTO tbl_credentials(canId, fileName) VALUES(?, ?);";
		conn = DBUtil.getConnection();
		int i = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, t.getCanId());
			ps.setString(2, t.getFileName());
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1 ? true : false;
	}

	@Override
	public boolean delete(TblCredentials t) {
		String sql = "DELETE FROM tbl_credentials WHERE creId IN(" + t.getIds() + ");";
		int i = 0;
		try {
			conn = DBUtil.getConnection();

			ps = conn.prepareStatement(sql);
			i = ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i > 0 ? true : false;
	}

	@Override
	public boolean modify(TblCredentials t) {
		String sql = "UPDATE tbl_credentials SET fileName = ? WHERE creId = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);

			ps.setString(1, t.getFileName());
			ps.setInt(2, t.getCreId());

			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1 ? true : false;
	}

	@Override
	public List<TblCredentials> get() {
		return null;
	}

	@Override
	public List<TblCredentials> get(TblCredentials cre) {
		String sql = "SELECT * FROM tbl_credentials WHERE canId = ?;";
		List<TblCredentials> list = new ArrayList<TblCredentials>();
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, cre.getCanId());
			rs = ps.executeQuery();
			while (rs.next()) {
				TblCredentials c = new TblCredentials();
				c.setCreId(rs.getInt("creId"));
				c.setFileName(rs.getString("fileName"));
				list.add(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

}
